| Conditions | 36 |
| Paths | > 20000 |
| Total Lines | 165 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like function.js ➔ initCalendar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /** |
||
| 111 | function initCalendar() {
|
||
| 112 | if (!year && !month && !day) {
|
||
| 113 | /* Called for first time */ |
||
| 114 | if (window.opener.dateField.value) {
|
||
| 115 | value = window.opener.dateField.value; |
||
| 116 | if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
|
||
| 117 | if (window.opener.dateType == 'datetime') {
|
||
| 118 | parts = value.split(' ');
|
||
| 119 | value = parts[0]; |
||
| 120 | |||
| 121 | if (parts[1]) {
|
||
| 122 | time = parts[1].split(':');
|
||
| 123 | hour = parseInt(time[0],10); |
||
| 124 | minute = parseInt(time[1],10); |
||
| 125 | second = parseInt(time[2],10); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | date = value.split("-");
|
||
| 129 | day = parseInt(date[2],10); |
||
| 130 | month = parseInt(date[1],10) - 1; |
||
| 131 | year = parseInt(date[0],10); |
||
| 132 | } else {
|
||
| 133 | year = parseInt(value.substr(0,4),10); |
||
| 134 | month = parseInt(value.substr(4,2),10) - 1; |
||
| 135 | day = parseInt(value.substr(6,2),10); |
||
| 136 | hour = parseInt(value.substr(8,2),10); |
||
| 137 | minute = parseInt(value.substr(10,2),10); |
||
| 138 | second = parseInt(value.substr(12,2),10); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) {
|
||
| 142 | dt = new Date(); |
||
| 143 | year = dt.getFullYear(); |
||
| 144 | month = dt.getMonth(); |
||
| 145 | day = dt.getDate(); |
||
| 146 | } |
||
| 147 | if (isNaN(hour) || isNaN(minute) || isNaN(second)) {
|
||
| 148 | dt = new Date(); |
||
| 149 | hour = dt.getHours(); |
||
| 150 | minute = dt.getMinutes(); |
||
| 151 | second = dt.getSeconds(); |
||
| 152 | } |
||
| 153 | } else {
|
||
| 154 | /* Moving in calendar */ |
||
| 155 | if (month > 11) {
|
||
| 156 | month = 0; |
||
| 157 | year++; |
||
| 158 | } |
||
| 159 | if (month < 0) {
|
||
| 160 | month = 11; |
||
| 161 | year--; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | if (document.getElementById) {
|
||
| 166 | cnt = document.getElementById("calendar_data");
|
||
| 167 | } else if (document.all) {
|
||
| 168 | cnt = document.all["calendar_data"]; |
||
| 169 | } |
||
| 170 | |||
| 171 | cnt.innerHTML = ""; |
||
| 172 | |||
| 173 | str = "" |
||
| 174 | |||
| 175 | //heading table |
||
| 176 | str += '<table class="calendar"><tr><th width="50%">'; |
||
| 177 | str += '<form method="NONE" onsubmit="return 0">'; |
||
| 178 | str += '<a href="javascript:month--; initCalendar();">«</a> '; |
||
| 179 | str += '<select id="select_month" name="monthsel" onchange="month = parseInt(document.getElementById(\'select_month\').value); initCalendar();">'; |
||
| 180 | for (i =0; i < 12; i++) {
|
||
| 181 | if (i == month) selected = ' selected="selected"'; |
||
| 182 | else selected = ''; |
||
| 183 | str += '<option value="' + i + '" ' + selected + '>' + month_names[i] + '</option>'; |
||
| 184 | } |
||
| 185 | str += '</select>'; |
||
| 186 | str += ' <a href="javascript:month++; initCalendar();">»</a>'; |
||
| 187 | str += '</form>'; |
||
| 188 | str += '</th><th width="50%">'; |
||
| 189 | str += '<form method="NONE" onsubmit="return 0">'; |
||
| 190 | str += '<a href="javascript:year--; initCalendar();">«</a> '; |
||
| 191 | str += '<select id="select_year" name="yearsel" onchange="year = parseInt(document.getElementById(\'select_year\').value); initCalendar();">'; |
||
| 192 | for (i = year - 25; i < year + 25; i++) {
|
||
| 193 | if (i == year) selected = ' selected="selected"'; |
||
| 194 | else selected = ''; |
||
| 195 | str += '<option value="' + i + '" ' + selected + '>' + i + '</option>'; |
||
| 196 | } |
||
| 197 | str += '</select>'; |
||
| 198 | str += ' <a href="javascript:year++; initCalendar();">»</a>'; |
||
| 199 | str += '</form>'; |
||
| 200 | str += '</th></tr></table>'; |
||
| 201 | |||
| 202 | str += '<table class="calendar"><tr>'; |
||
| 203 | for (i = 0; i < 7; i++) {
|
||
| 204 | str += "<th>" + day_names[i] + "</th>"; |
||
| 205 | } |
||
| 206 | str += "</tr>"; |
||
| 207 | |||
| 208 | var firstDay = new Date(year, month, 1).getDay(); |
||
| 209 | var lastDay = new Date(year, month + 1, 0).getDate(); |
||
| 210 | |||
| 211 | str += "<tr>"; |
||
| 212 | |||
| 213 | dayInWeek = 0; |
||
| 214 | for (i = 0; i < firstDay; i++) {
|
||
| 215 | str += "<td> </td>"; |
||
| 216 | dayInWeek++; |
||
| 217 | } |
||
| 218 | for (i = 1; i <= lastDay; i++) {
|
||
| 219 | if (dayInWeek == 7) {
|
||
| 220 | str += "</tr><tr>"; |
||
| 221 | dayInWeek = 0; |
||
| 222 | } |
||
| 223 | |||
| 224 | dispmonth = 1 + month; |
||
| 225 | |||
| 226 | if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
|
||
| 227 | actVal = "" + formatNum4(year) + "-" + formatNum2(dispmonth, 'month') + "-" + formatNum2(i, 'day'); |
||
| 228 | } else {
|
||
| 229 | actVal = "" + formatNum4(year) + formatNum2(dispmonth, 'month') + formatNum2(i, 'day'); |
||
| 230 | } |
||
| 231 | if (i == day) {
|
||
| 232 | style = ' class="selected"'; |
||
| 233 | current_date = actVal; |
||
| 234 | } else {
|
||
| 235 | style = ''; |
||
| 236 | } |
||
| 237 | str += "<td" + style + "><a href=\"javascript:returnDate('" + actVal + "');\">" + i + "</a></td>"
|
||
| 238 | dayInWeek++; |
||
| 239 | } |
||
| 240 | for (i = dayInWeek; i < 7; i++) {
|
||
| 241 | str += "<td> </td>"; |
||
| 242 | } |
||
| 243 | |||
| 244 | str += "</tr></table>"; |
||
| 245 | |||
| 246 | cnt.innerHTML = str; |
||
| 247 | |||
| 248 | // Should we handle time also? |
||
| 249 | if (window.opener.dateType != 'date' && !clock_set) {
|
||
| 250 | |||
| 251 | if (document.getElementById) {
|
||
| 252 | cnt = document.getElementById("clock_data");
|
||
| 253 | } else if (document.all) {
|
||
| 254 | cnt = document.all["clock_data"]; |
||
| 255 | } |
||
| 256 | |||
| 257 | str = ''; |
||
| 258 | init_hour = hour; |
||
| 259 | init_minute = minute; |
||
| 260 | init_second = second; |
||
| 261 | str += '<fieldset>'; |
||
| 262 | str += '<form method="NONE" class="clock" onsubmit="returnDate(\'' + current_date + '\')">'; |
||
| 263 | str += '<input id="hour" type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_hour, \'hour\'); init_hour = this.value;" value="' + formatNum2(hour, 'hour') + '" />:'; |
||
| 264 | str += '<input id="minute" type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_minute, \'minute\'); init_minute = this.value;" value="' + formatNum2(minute, 'minute') + '" />:'; |
||
| 265 | str += '<input id="second" type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_second, \'second\'); init_second = this.value;" value="' + formatNum2(second, 'second') + '" />'; |
||
| 266 | str += ' '; |
||
| 267 | str += '<input type="submit" value="' + submit_text + '"/>'; |
||
| 268 | str += '</form>'; |
||
| 269 | str += '</fieldset>'; |
||
| 270 | |||
| 271 | cnt.innerHTML = str; |
||
| 272 | clock_set = 1; |
||
| 273 | } |
||
| 274 | |||
| 275 | } |
||
| 276 | |||
| 535 | } |